const express = require('express');
const router = express.Router(); // 路由器
const LunboModel=require('../models/lunbo');
const ProDetailModel=require('../models/proDetail');
const FactoryPicModel=require('../models/factoryPic');
const CategoryModel=require('../models/category');
const NewsModel=require('../models/news');
const HFModel=require('../models/hf');
const moment=require('moment')

//前台访问url地址
router.get('/',async (req,res)=>{
    //轮播图查询
    const lunbo = await LunboModel.find();

   //  let ProDetailModelObj=new ProDetailModel({
   //      title: '手工清洁剂',
   //      popular: 0,
   //      description: '这是手工清洁剂',
   //      pic: '',
   //  })
   // await ProDetailModelObj.save();

    // console.log("ProDetailModelObj", ProDetailModelObj);


    //产品查询
    let products=await ProDetailModel.find();

    // console.log("products",products);
    if(products.length>8){
        products=products.slice(0,8);
    }



    //工厂照片查询
    // let FactoryPicModelObj=new FactoryPicModel({
    //     title: "工厂照片",
    //     src: '1',
    //     isLock: 0,
    //     hot: 0
    // });
    // await FactoryPicModelObj.save();
    // console.log("FactoryPicModelObj", FactoryPicModelObj);
    let factoryPic=await FactoryPicModel.find();

    // console.log("factoryPic",factoryPic);
    if(factoryPic.length>5){
        factoryPic=factoryPic.slice(0,5);
    }

    //新闻中心查询
    //分类
    // let CategoryModelObj=new CategoryModel({
    //     cateName:'常见问题'
    // });
    // await CategoryModelObj.save();
    // console.log("CategoryModelObj", CategoryModelObj);
    let category=await CategoryModel.find();
    // console.log("category", category);

    //新闻
   //  let NewsModelObj=new NewsModel({
   //      title: '热熔胶正确的使用方法',
   //
   //      // cateId: '612b456e12f90e3d0dd0ba13',
   //      // cateId: '612b454fdaf430cbe09a1181',
   //      cateId: '612b45800c98843ee12d4165',
   //
   //      viewcount: 2,
   //
   //      descption: '新闻详情',
   //  })
   //  console.log("NewsModelObj", NewsModelObj);
   // await NewsModelObj.save();
   let news=await NewsModel.find();
    // console.log("news", news);
    let newsArray1=[];
    let newsArray2=[];
    let newsArray3=[];
    // let newsArray1Title=category[0].cateName;
    // let newsArray2Title=category[1].cateName;
    // let newsArray3Title=category[2].cateName;
    for(let i=0;i<category.length;i++){
        let k=0;
        for(let j=0;j<news.length;j++){
            if(category[i]._id.equals(news[j].cateId)){
                if(k==4){
                    break;
                }else {
                    if(i==0){
                        newsArray1[k++]=news[j];
                    }else if(i==1){
                        newsArray2[k++]=news[j];
                    }else if(i==2){
                        newsArray3[k++]=news[j];
                    }
                }
            }
        }
    }
    // console.log(newsArray1);
    // console.log(newsArray2);
    // console.log(newsArray3);
    // console.log(newsArray1Title);
    // console.log(newsArray2Title);
    // console.log(newsArray3Title);


    //基本信息
    let baseInfo=await HFModel.findOne();
    // console.log(baseInfo);
    res.render('./front/index',{baseInfo,lunbo,products,factoryPic,category,newsArray1,newsArray2,newsArray3,moment});
})

module.exports = router;
